package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.MyTheadLocal;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/shoppingCart")
@RestController
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R addShoppingCart(@RequestBody ShoppingCart shoppingCart){

        Long userId = MyTheadLocal.get();

        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();

        qw.eq(ShoppingCart::getUserId, userId);

        if (shoppingCart.getDishId() != null){

            qw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());

        }else {

            qw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        ShoppingCart one = shoppingCartService.getOne(qw);

        if (one == null){

            shoppingCart.setNumber(1);

            shoppingCart.setUserId(userId);

            one = shoppingCart;

            shoppingCartService.save(one);

        }else {

            Integer number = one.getNumber();

            System.out.println(number);

            one.setNumber(number+1);

            shoppingCartService.updateById(one);

        }

        return R.success(one);

    }

    @GetMapping("/list")
    public R listShoppingCart(){

        Long userId = MyTheadLocal.get();

        // select * from shoping_cart where user_id = ?

        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();

        qw.eq(ShoppingCart::getUserId,userId);

        List<ShoppingCart> list = shoppingCartService.list(qw);


        return R.success(list);
    }

    @PostMapping("/sub")
    public R subShoppingCart(@RequestBody ShoppingCart shoppingCart){

        Long userId = MyTheadLocal.get();

        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();

        qw.eq(ShoppingCart::getUserId,userId);

        if (shoppingCart.getDishId() != null){

            qw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());

            }else {

            qw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());

        }

        ShoppingCart one = shoppingCartService.getOne(qw);

        Integer number = one.getNumber();

        if (number == 0){

            shoppingCartService.remove(qw);

        }else {

            one.setNumber(one.getNumber()-1);

            shoppingCartService.updateById(one);

        }

        return R.success(one);

    }

    @DeleteMapping("/clean")
    public R cleanShoppingCart(){

        Long userId = MyTheadLocal.get();

        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();

        qw.eq(ShoppingCart::getUserId,userId);

        shoppingCartService.remove(qw);

        return R.success(null);

    }

}
